using System;
using Widgetsphere.Core.DataAccess;
using System.Configuration;

namespace ACME.Northwind.Business
{
		/// <summary>
		/// This is a static class used to define the database connection properties
		/// </summary>
	public class ConfigurationValues
	{
		private static ConfigurationValues _instance;

		private string mConnectionString = string.Empty;
		private string mDataAccessServiceUrl = string.Empty;
		private int _defaultTimeOut = 30;

		private ConfigurationValues()
		{
			try
			{
				ConfigurationFile cf = new ConfigurationFile();
				if(cf.DirectConnect)
					mConnectionString = cf.ConfigString;
				else
					mDataAccessServiceUrl = cf.ConfigString;
			}
			catch{}
			try
			{
				mConnectionString = ConfigurationManager.ConnectionStrings["ACME.Northwind"].ConnectionString;
			}
			catch { }
		}

		/// <summary>
		/// Returns a singleton configuration object.
		/// </summary>
		public static ConfigurationValues GetInstance()
		{
			if (_instance == null)
				_instance = new ConfigurationValues();
			return _instance;
		}

		/// <summary>
		/// Determines the connection string for all static select methods.
		/// </summary>
		public string ConnectionString
		{
			get { return mConnectionString; }
			set { mConnectionString = value; }
		}

		/// <summary>
		/// Determines if the web service is used (remote conection) or this is a direct database connection.
		/// </summary>
		[Obsolete("This property no longer applies.")]
		public bool DirectConnect
		{
			get
			{
				if(this.mDataAccessServiceUrl == string.Empty) return true;
				else return false;
			}
		}

		/// <summary>
		/// The URL to the web servive that is used to pass database across the wire.
		/// </summary>
		[Obsolete("This property no longer applies.")]
		public string DataAccessServiceUrl
		{
			get { return mDataAccessServiceUrl; }
			set { mDataAccessServiceUrl = value; }
		}

		/// <summary>
		/// The timeout in seconds of the database connection.
		/// </summary>
		public int DefaultTimeOut
		{
			get { return _defaultTimeOut; }
			set
			{
				if (value < 0)
					value = 0;
				_defaultTimeOut = value;
			}
		}

		/// <summary>
		/// Determines the version of the model that created this library.
		/// </summary>
		public string Version
		{
			get { return "0.0.0.0"; }
		}

		/// <summary>
		/// Determines if the database version matches the current library model version.
		/// </summary>
		public bool IsDataBaseVersionMatch()
		{
			string sql = "SELECT CAST(p.value AS sql_variant) AS [Value] FROM sys.extended_properties AS p WHERE (p.name=N'dbVersion')";
			System.Data.DataSet ds = DatabaseHelper.ExecuteSql(this.ConnectionString, sql, new System.Data.SqlClient.SqlParameter[] { });
			string dbVersion = "";
			if (ds.Tables.Count == 1)
				dbVersion = (string)ds.Tables[0].Rows[0][0];
			return Version == dbVersion;
		}

		internal static string GetSQLTableMap(string original, string baseColumnSQL, string newAlias, string fieldName)
		{
			if (!newAlias.Contains("[")) newAlias = "[" + newAlias + "]";
			if (!fieldName.Contains("[")) fieldName = "[" + fieldName + "]";
			return original.Replace(baseColumnSQL, newAlias + "." + fieldName + "");
		}

	}
}
